﻿using AuthCenter_HuangJingtao.Domain;
using AuthCenter_HuangJingtao.ErrorCode;
using AuthCenter_HuangJingtao.Infrastructure.Imterfaces;
using AuthCenter_HuangJingtao.Read.Api.Application.Command.ReversoaLL;
using AuthCenter_HuangJingtao.Read.Api.Dto;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace AuthCenter_HuangJingtao.Read.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ReversoAllController : ControllerBase
    {
        IMediator mediator;

        public ReversoAllController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        [HttpPost]
        public async Task<ApiResult<int>> AddReversoaLL(Reverso Reverso)
        {
            try
            {
                AddReversoaLLCommand AddReversoaLLCommand = new AddReversoaLLCommand()
                {
                    Reverso = Reverso
                };
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var userName = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                var roleName = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
                var customerValue = HttpContext.User.FindFirst("CustomClaimType")?.Value;
                var key = HttpContext.User.FindFirst("Key")?.Value;

                return await mediator.Send(AddReversoaLLCommand);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        public async Task<ApiResult<ApiPeaging<List<Reverso>>>> ShowReverso(string? reverso_Name, string? reverso_State, DateTime? reverso_CreateTime,int Index,int Size,string? Name,int Id)
        {
            try
            {
                ShowReversoCommand ShowReversoCommand = new ShowReversoCommand() 
                {
                    reverso_Name= reverso_Name,
                    reverso_State= reverso_State,
                    reverso_CreateTime= reverso_CreateTime,
                    Index= Index,
                    Size= Size,
                    Name= Name,
                    Role_Id= Id
                };
                return await mediator.Send(ShowReversoCommand);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpPost]
        public async Task<ApiResult<int>> UpdateReverso(Reverso Reverso)
        {
            try
            {
                UpdateReversoCommand UpdateReversoCommand = new UpdateReversoCommand() 
                {
                    Reverso = Reverso
                };
                return await mediator.Send(UpdateReversoCommand);   
            }
            catch (Exception)
            {
                throw;
            }
        }
        [HttpGet]
        public async Task<ApiResult<int>> DelReverso(int id)
        {
            try
            {
                DelReversoCommand DelReversoCommand = new DelReversoCommand() 
                {
                    Id=id
                };
                return await mediator.Send(DelReversoCommand);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        public async Task<ApiResult<Reverso>> FirstReverso(int id)
        {
            try
            {
                FirstReversoCommand FirstReversoCommand = new FirstReversoCommand() 
                {
                    id= id
                };
                return await mediator.Send(FirstReversoCommand);
            }
            catch (Exception)
            {

                throw;
            }
        }
        //匿名

        [AllowAnonymous]
        [HttpGet]
        public async Task<ApiResult<ShowDto>> Login(string? Name,string? PassWord)
        {
            try
            {
                LoginCommand LoginCommand=new LoginCommand()
                {
                    Name= Name,
                    Password= PassWord
                };
                return await mediator.Send(LoginCommand);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
